<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

const  panel = ""; // 宝塔接口

const btkey = ""; // 宝塔key


/**
 * 获取目录下文件
 * 
 * 作者@ qq32579136
 * web@ https://www.pmhapp.com
**/

function get_file($dir){

    //先判断指定的路径是不是一个文件夹
    
    if (is_dir($dir)){
    
        if ($dh = opendir($dir)){
        
            while (($file = readdir($dh))!= false){
            
            //文件名的全路径 包含文件名
            $filePath = $dir.'/'.$file;
            
            $data[] = $filePath;
            
            }
        
        closedir($dh);
        
        }
    
    }
    return $data;
}

/**
 * 返回Json
 * 作者@ qq32579136
 * web@ https://www.pmhapp.com
**/
function json_exit($code,$msg,$data = ''){
    exit(json_encode(['code'=>$code,'msg'=>$msg,'data'=>isset($data)?$data:''],JSON_UNESCAPED_UNICODE));
}

/**
 * Telegram 提取群昵称
 * 作者@ qq32579136
 * web@ https://www.pmhapp.com
**/
function crawler_name($url){
    
    $url_exists  = url_exists($url);
    if(!$url_exists){
        return "获取失败";
    }
    header("content-type:text/html;charset=utf-8");
    header('Access-Control-Allow-Origin:*');
    $html = file_get_contents($url);
    preg_match_all('/<meta property="og:title" content="(.*?)">/is', $html, $name);
    if(isset($name[1][0])){
        return $name[1][0];
    }
    
}

/**
 * 处理数组
 * 作者@ qq32579136
 * web@ https://www.pmhapp.com
**/
function HandleArrays($data){
    $data = html_entity_decode($data);
    $data = stripslashes($data);  
    return $data;
}

/**
 * 制作状态
 * 作者@ qq32579136
 * web@ https://www.pmhapp.com
**/
function HandleType(array $data){
    $datas=[];
    foreach ($data as $key){
        $_data['qunId'] = $key;
        $_data['type'] = 0;
        array_push($datas,$_data);
    }
    return json_encode($datas);
}

/**
 * 判断网页是否存在
 * 
 * 作者@ qq32579136
 * web@ https://www.pmhapp.com
 * 返回false or 1
 * 
**/
function url_exists($url) {

 $hdrs = @get_headers($url);

 return is_array($hdrs) ? preg_match('/^HTTP\\/\\d+\\.\\d+\\s+2\\d\\d\\s+.*$/',$hdrs[0]) : false;

}



/**
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * | 宝塔操作分割线
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * | 作者@ qq32579135
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 * | 官网：https://www.pmhapp.com
 * +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
** /

//创建定时任务
/**
name: 123
type: minute-n
where1: 1
hour: 
minute: 
week: 
sType: toUrl
sBody: undefined
sName: 
backupTo: localhost
save: 
sBody: undefined
urladdress: http://jk.cddue312.com/
save_local: undefined
notice: undefined
notice_channel: undefined
**/
function TimingTask($name,$_url){
  	//拼接URL地址
	$url = panel.'/crontab?action=AddCrontab';  
	//准备POST数据
	$p_data = GetKeyData();		//取签名
	$p_data['name'] = $name;
	$p_data['type'] = 'minute-n';
	$p_data['where1'] = 1;
	$p_data['hour'] = '';
	$p_data['minute'] = '';
	$p_data['week'] = '';
	$p_data['sType'] = 'toUrl';
	$p_data['sBody'] = 'undefined';
	$p_data['sName'] = '';
	$p_data['backupTo'] = 'localhost';
	$p_data['save'] = '';
	$p_data['sBody'] = 'undefined';
	$p_data['urladdress'] = $_url;
	$p_data['save_local'] = 'undefined';
	
	
	//请求面板接口
	$result = HttpPostCookie($url,$p_data);
	
	//解析JSON数据
	$data = json_decode($result,true);
  	return $data;
	
}

/**
 * 暂停定时器
 * //crontab?action=set_cron_status
**/
function DeleteTimingTask($id){
  	//拼接URL地址
	$url = panel.'/crontab?action=set_cron_status';  
	//准备POST数据
	$p_data = GetKeyData();	
	$p_data['id'] = $id;
	
	//请求面板接口
	$result = HttpPostCookie($url,$p_data);
	
	//解析JSON数据
	$data = json_decode($result,true);
  	return $data;	
}

  //示例取面板日志	
function GetLogs(){
	//拼接URL地址
	$url = panel.'/data?action=getData';
	
	//准备POST数据
	$p_data = GetKeyData();		//取签名
	$p_data['table'] = 'logs';
	$p_data['limit'] = 10;
	$p_data['tojs'] = 'test';
	
	//请求面板接口
	$result = HttpPostCookie($url,$p_data);
	
	//解析JSON数据
	$data = json_decode($result,true);
  	return $data;
}

function GetKeyData(){
  	$now_time = time();
	$p_data = array(
		'request_token'	=>	md5($now_time.''.md5(btkey)),
		'request_time'	=>	$now_time
	);
	return $p_data;    
}


/**
 * 发起POST请求
 * @param String $url 目标网填，带http://
 * @param Array|String $data 欲提交的数据
 * @return string
 */
function HttpPostCookie($url, $data,$timeout = 60)
{
	//定义cookie保存位置
    $cookie_file='./'.md5(panel).'.cookie';
    if(!file_exists($cookie_file)){
        $fp = fopen($cookie_file,'w+');
        fclose($fp);
    }
	
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
    curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
}


